//
//  TradeRecordsViewController.m
//  阿帕
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "MyCommentsViewController.h"
#import "MyCommentCell.h"
#import "PingJiaDetailViewController.h"
#import "SGPagingView.h"

@interface MyCommentsViewController ()<UITableViewDataSource, UITableViewDelegate, SGPageTitleViewDelegate>

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, copy) NSString *commentType;

@end

static NSString *const cellID = @"MyCommentCellID";

static const CGFloat cellHeight = 130;

@implementation MyCommentsViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.pageNum = 1;
    _commentType = @"1";
    
    [self setupBaseView];
    
}

- (void)setupBaseView {
    
    self.view.backgroundColor = BackgroundColor;
    
    [self createSegmentedControl];
    
    [self.view addSubview:self.tableView];
}

- (void)createSegmentedControl {
    NSArray *titleArr = @[@"我的评价", @"评价我的"];
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.indicatorStyle = SGIndicatorStyleCover;
    configure.indicatorColor = Theme_ColorValue;
    configure.titleColor = Theme_ColorValue;
    configure.indicatorHeight = SY(30);
    configure.indicatorAdditionalWidth = 120;
    configure.titleSelectedColor = [UIColor whiteColor];
    /// pageTitleView
    SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, 270, 30) delegate:self titleNames:titleArr configure:configure];
    pageTitleView.layer.borderWidth = 1;
    pageTitleView.layer.borderColor = Theme_ColorValue.CGColor;
    pageTitleView.layer.cornerRadius = 3;
    pageTitleView.layer.masksToBounds = YES;
    pageTitleView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = pageTitleView;
}

//按钮点击事件
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    
    NSLog(@"1111111111111111111");
    
    switch (selectedIndex) {
        case 0:
            _commentType = @"1";
            break;
        case 1:
            _commentType = @"0";
            break;
            
        default:
            break;
    }
    
    self.pageNum = 1;
    
    [self commentRecordsListRequest];
}
- (void)commentRecordsListRequest {
    
    WEAKSELF
    
    if (self.pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"commentType": _commentType,
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDERCOMMENT_MYCOMMENT) withParameter:params withVC:self netName:@"用户评价列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {

            [weakSelf.dataArray addObjectsFromArray:[MyCommentRecordModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];

            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        } else {
            
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }

            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];

        }

        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self commentRecordsListRequest];
}

- (void)loadMoreData{
    _pageNum ++;
    [self commentRecordsListRequest];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCommentRecordModel *model = self.dataArray[indexPath.row];
    
    return [self calculateTableViewCellHeight:model];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCommentRecordModel *model = self.dataArray[indexPath.row];
    PingJiaDetailViewController *pjdvc = [[PingJiaDetailViewController alloc] init];
    pjdvc.sourceType = 1;
    pjdvc.commentType = _commentType;
    pjdvc.orderCode = model.code;
    [self.navigationController pushViewController:pjdvc animated:YES];
}

#pragma mark - 计算cell高度
- (CGFloat)calculateTableViewCellHeight:(MyCommentRecordModel *)model {
    
    CGSize size = [DCSpeedy dc_calculateTextSizeWithText:model.commentContent WithTextFont:14 WithMaxW:ScreenW - 70];
    if (model.imageUrlList.count == 0) {
        //无图
        return cellHeight + size.height;
    } else {
        //有图
        return cellHeight + size.height + 85;
    }
    return 0;
}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - MenuBarHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyCommentCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_centerY -= 70;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}


@end
